package org.james.mytest.quartz;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class Test {

	public static void main(String[] args) throws SchedulerException {
		SchedulerFactory factory = new StdSchedulerFactory();
		// 从工厂里面拿到一个scheduler实例
		Scheduler scheduler = factory.getScheduler();
		// 计算任务的开始时间，DateBuilder.evenMinuteDate方法是取下一个整数分钟
		Date runTime = DateBuilder.evenMinuteDate(new Date());
		// 真正执行的任务并不是Job接口的实例，而是用反射的方式实例化的一个JobDetail实例
		JobDetail job = JobBuilder.newJob(Job1.class).withIdentity("job1", "group1").build();
		job.getJobDataMap().put("param1", "caonima");
		// 定义一个触发器，startAt方法定义了任务应当开始的时间
		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
				.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?")).build();
		
//		System.out.println("1params :" + trigger.getJobDataMap().getString("param1"));
//		trigger.getJobDataMap().put("param1", "caonima");
//		System.out.println("2params :" + trigger.getJobDataMap().getString("param1"));
		// 将任务和Trigger放入scheduler
		scheduler.scheduleJob(job, trigger);
		scheduler.start();
		// scheduler结束
		scheduler.shutdown(true);
	}

}
